//这里仅示范npm安装方式的引入，其它方式引入请看最上面【安装】部分
import Router from 'uni-simple-router'
import store from '../store/index.js'
//初始化
console.log("/router/index:初始化可用路由集合")
console.table(ROUTES)

const router = new Router({
    routes:ROUTES //路由表
});
//全局路由前置守卫
router.beforeEach(async (to, from, next) => {
  const token = store.getters.getToken
  const user = store.getters.getUser
  const tabbers = store.getters.getTabbars
  if(token){
	  if(to.path==="/pages/login/login"){
		  next({path:"/pages/index/index"})
	  }else{
		  if(user){
			  await store.dispatch("add_tabbars",tabbers)
			  next()
		  }else{
			  try{
				 const {authorities} = await store.dispatch('get_user')
				  await store.dispatch("add_tabbars",authorities)
				  next()
			  }catch(error){
				  uni.showToast({
				  	title:error||"未知错误",
					duration:1500
				  })
				  console.error(error)
				  await store.dispatch('clear_user')
				  next({path:"/pages/login/login"});
			  }
		  }
	  }
  }else{
	  console.error('没有登录')
	  if(to.path==="/pages/login/login"){
		  next()
	  }else{
		  next({path:"/pages/login/login"});
	  }
	  
  }
})
// 全局路由后置守卫
router.afterEach((to, from) => {
})
export default router;